//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    sendTime: '获取验证码',
    sendColor: '#FF2823',
    snsMsgWait: 60,
    navHeight: app.globalData.navHeight,
    navTitleTop: app.globalData.navTitleTop,
    code: '',
    tel:''
  },
  inputTel: function(e) {
    this.setData({
      tel: e.detail.value
    })
  },
  inputCode: function(e) {
    this.setData({
      code: e.detail.value
    })
  },
  // 获取验证码
  sendCode() {
    if (this.data.tel == "") {
      app.showMsg('请输入手机号');
      return;
    }
    if (!(/^1[3|4|5|8][0-9]\d{8}$/.test(this.data.tel))) {
      app.showMsg('手机号输入错误');
      return;
    }
    let phone =this.data.tel
    // 60秒后重新获取验证码
    let inter = setInterval(function () {
        this.setData({
          smsFlag: true,
          sendColor: '#cccccc',
          sendTime: this.data.snsMsgWait + 's后重发',
          snsMsgWait: this.data.snsMsgWait - 1
        });
      if (this.data.snsMsgWait < 0) {
        clearInterval(inter)
        this.setData({
          sendColor: '#FF2823',
          sendTime: '获取验证码',
          snsMsgWait: 60,
          smsFlag: false
        });
      }
    }.bind(this), 1000);
    app._post_form('/blade-auth2/sendCode', {phone}, (res) => {
      if (res) {
        app.showMsg(res.msg);
      }
    })
  },

  // 提交信息
  saveClick() {
    if (this.data.tel == "") {
      app.showMsg("手机号不可为空");
      return;
    }
    if (this.data.code == "") {
      app.showMsg("验证码不可为空");
      return;
    }
    let phone=this.data.tel
    let code=this.data.code
    app._post_form('/blade-auth2/genToken', {phone,code}, (res) => {
      app.showMsg(res.msg);
      if (res.success) {
        wx.setStorageSync('studentList',res.data.students);
        wx.setStorageSync('sign', res.data.accessToken);
        wx.switchTab({
          url: '/pages/index/index'
        })
      }
    })
  }
})
